// 文档结构载入完成后，显示Loading效果
$(function(){
  yyy.init();
});
// 文档元素载入完成后，删除Loading效果
// $(window).on('load',function(){
//     $("#loading_container").fadeOut(500,function(){
//         $(this).remove();
//     });
// });

var yyyFn = function(){
  this.mid = mid||'DJEHBSUAAY';
  var webapi = "https://api-shake.xinglongdayuan.com";
  this.yyy = JSON.parse(localStorage.getItem('queenActivity')) || {};
  this.API = {
    enterpriseList: webapi + '/ol/shake/enterpriseList/' + this.mid, // 获取领取明细
    setEnterprise: webapi + '/ol/shake/setEnterprise/' + this.mid, // 获取领取明细
    takeActivity: webapi + '/ol/shake/takeActivity/' + this.mid, // 获取领取明细
    shake_detail: webapi + "/ol/shake/shake_detail/" + this.mid,//判断登录
    shake_member: webapi + "/ol/shake/getSharkUser/" + this.mid//会员信息
    
    
  };
  this.$noData = '<div style="padding:20px 0 40px;width:100%;line-height:2;text-align:center;">您还没有兑换过哦~<br>快去兑换吧</div>'
  this.receiptsData = undefined;
  this.isApp = CYutils.cookie.get('BDed_third_party_wxweb')==='app';
  this.isWeiXin = CYutils.isWeiXin();
  this.isAndroid = CYutils.isAndroid();
  this.isIOS = CYutils.isIOS();
  this.bridge = null; //苹果jsBridge
  this.outletId = null; // 已选门店ID
  this.shareData = {
      title:'',
      desc:'',
      link:'/app/invite/',
      imgUrl:''
  };
  return this;
};

//请求------------------
webApp.post = function (url, parameter, callback) {
  parameter = mui.extend({}, parameter);
  $.ajax({
      tpl: $('<div id="loadingToast" style="opacity: 1; display: block;">' +
          '<div class="weui-mask_transparent"></div>' +
          '<div class="weui-toast">' +
          '<i class="weui-loading weui-icon_toast"></i>' +
          '<p class="weui-toast__content">数据加载中</p>' +
          '</div>' +
          '</div>'),
      beforeSend: function(){this.tpl.appendTo('body')},
      complete: function(){try{this.tpl.remove()}catch(f){}},
      url: url,
      dataType: 'jsonp',
      data: parameter,
      jsonp: 'callback',
      success: function (data) {
          if (data) {
              if (data.Error == 0) {
                  callback(data);
              }else {
                  callback(data);
              }
          }
      },
      error:function(){
      }
  });
}
yyyFn.prototype = {
  init: function(){
      var _this = this;
      this.shakeStart();
      // CYutils.wxShare(this.shareData);
      this.bindEvents();
  },
   shakeStart: function() {
    var _this = this;
    // ----------活动信息--------
    webApp.post(_this.API.shake_detail,{},function(data){
        if(data.Error==0){
            canUseShake=true;
            shuju = data;
            shuju.id = data.data.id;
            // ----------会员信息-------------
            webApp.post(_this.API.shake_member,{id:shuju.id},function(data){
                console.log(data)
                if(data.Error==0){
                
                    // document.getElementById('cjju').innerHTML = allcount;
                    jQuery('#point').html(data.data.point);
                    var ss= data.data.pointNum + '次';
                    jQuery('#pointnum').html(ss);

                }else if(data.data == '' || data.data.length<1){
                    mui.alert(data.Msg,'温馨提示',function(){
                        location.href = ' https://user.wx.xinglongdayuan.com/jumpurl?acurl=https://wx.shake.xinglongdayuan.com/yaoyiyao/queenActivity.html&comCode=1';
                    });
                }else{

                    mui.toast(data.Msg);
                }
            });
        }else if(data.Error==6009){
            // window.location.href = '/index.html';
            // window.location.href = '/indexnewyear.html';
        }else if(data.Error==6007){
            CYutils.dialog('请先登录', {
                onConfirm: function () {
                    if (CYutils.isWeiXin()) {
                        window.location.href = 'https://user.wx.xinglongdayuan.com/jumpurl?acurl=https://wx.shake.xinglongdayuan.com/yaoyiyao/queenActivity.html&comCode=1';
                    }else {

                        if (CYutils.isAndroid()) {
                            Android.login('location.reload', 'location.reload');
                        } else if (CYutils.isIOS()) {
                            // mui.toast('isIOS');
                            bridge.send({Foo: "login", "loginResultFunction": 'location.reload'});
                        }else {
                            CYutils.dialog('请使用微信或兴隆大院App访问', {
                                onConfirm: function () {
                                }
                            });
                        }
                    }
                }
            });
            //
            //     }
            // });

            // mui.toast(data.Msg+'121212');
        }else {
            mui.toast(data.Msg);
        }
    });
},
  initData: function(){
      var _this = this;
      webApp.post(_this.API.enterpriseList,{},function(data){
          if(data.Error == '101'){
              CYutils.dialog(data.Msg, {
                  onConfirm: function(){
                      if(_this.isApp){
                          if(_this.isAndroid){
                              Android.login('location.reload');
                          }else if(_this.isIOS){
                              bridge.send({ Foo:"login","loginResultFunction":'location.reload'});
                          }
                      } else if(_this.isWeiXin){
                          window.location.href = '/app/eshop/register';
                      } else {
                          CYutils.dialog('请使用微信或兴隆大院App访问',{
                              onConfirm: function(){}
                          });
                      }
                  }
              });
              return
          }
          if(data.Error!=0) return;
          var data = data.data;
          $("#outletList").html('');
          for (var j=0; j<data.length; j++) {
                  $("#outletList").append('<div class="menu-list-item" data-id="'+data[j].id+'" data-title="'+data[j].title+'"><div>'+data[j].title+'</div></div>')
              
          }
          _this.initDom();
          setTimeout(function(){CYutils.loading();},500);
      });
  },
  enterpriseList: function(){
      return CYutils.load(this.API.enterpriseList,{});
  },

  initDom: function(){
      if(this.yyy.outletId) {
          document.getElementById('outletTitle').innerHTML = this.yyy.outletTitle;
          $('#outletList [data-id='+this.yyy.outletId+']').addClass('selected');
      }
      this.bindEvents();
  },
  bindEvents: function(){
      var _this = this;
      var $outletList = $('#outletList');
      // //选择门店
      $('#outletList').on('click','.menu-list-item',function(){
          var $this = $(this);
          $outletList.find('.menu-list-item').not($this).removeClass('selected');
          $this.addClass('selected');
          // _this.yyy.outletId = this.dataset.id;
          // _this.yyy.outletTitle = this.dataset.title;
          // localStorage.setItem('yyy', JSON.stringify(_this.yyy));
          // window.location.href = '/yyy.html';
          // $('#outletTitle').html(this.dataset.title);
          webApp.post(_this.API.setEnterprise,{id:this.dataset.id,name:this.dataset.title},function(data){
            if(data.Error!=0) return;
            window.location.href = '/queenActivity.html';
        });

      });

      jQuery('#chooseOutlet').click(function () {

        // jQuery('#outletPopup').show();

        webApp.post(_this.API.takeActivity,{},function(data){
            if(data.Error!=0) return;
            window.location.href = '/queenActivity.html';
        });
      })

      jQuery('#close').click(function () {

        jQuery('#outletPopup').hide();
      })

      //弹窗show hide
      $('body').on('click','[data-popup]',function(){
          var _key = this.dataset.popup;
          if (!_this[_key]) {
              _this[_key] = CYutils.popup(_key)
          }
          _this[_key].show();
          $(_key+'Close').off().on('click',function(){
              _this[_key].hide();
          });
      });
  }
};

var yyy = new yyyFn();
